﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;

namespace l1cs.server.serverpackets
{
    public class S_DropItem : ServerBasePacket
    {
        private static Logger _log = Logger.getLogger( typeof( S_DropItem ).FullName );

        private byte[] _byte = null;

        public S_DropItem( L1ItemInstance item )
        {
            buildPacket( item );
        }

        private void buildPacket( L1ItemInstance item )
        {
            // int addbyte = 0;
            // int addbyte1 = 1;
            // int addbyte2 = 13;
            // int setting = 4;
            writeC( Opcodes.S_OPCODE_DROPITEM );
            writeH( item.getX() );
            writeH( item.getY() );
            writeD( item.getId() );
            writeH( item.getItem().getGroundGfxId() );
            writeC( 0 );
            writeC( 0 );
            if ( item.isNowLighting() )
            {
                writeC( item.getItem().getLightRange() );
            }
            else
            {
                writeC( 0 );
            }
            writeC( 0 );
            writeD( item.getCount() );
            writeC( 0 );
            writeC( 0 );
            if ( item.getCount() > 1 )
            {
                writeS( item.getItem().getName() + " (" + item.getCount() + ")" );
            }
            else
            {
                int itemId = item.getItem().getItemId();
                int isId = item.isIdentified() ? 1 : 0;
                if ( itemId == 20383 && isId == 1 )
                { // 騎馬用ヘルム
                    writeS( item.getItem().getName() + " [" + item.getChargeCount() + "]" );
                }
                else if ( ( itemId == 40006 || itemId == 40007
                      || itemId == 40008 || itemId == 40009
                      || itemId == 140006 || itemId == 140008 ) && isId == 1 )
                { // ワンド類
                    writeS( item.getItem().getName() + " (" + item.getChargeCount() + ")" );
                }
                else if ( item.getItem().getLightRange() != 0 && item.isNowLighting() )
                {
                    writeS( item.getItem().getName() + " ($10)" );
                }
                else
                {
                    writeS( item.getItem().getName() );
                }
            }
            writeC( 0 );
            writeD( 0 );
            writeD( 0 );
            writeC( 255 );
            writeC( 0 );
            writeC( 0 );
            writeC( 0 );
            writeH( 65535 );
            // writeD(0x401799a);
            writeD( 0 );
            writeC( 8 );
            writeC( 0 );
        }

        override
        public byte[] getContent()
        {
            if ( _byte == null )
            {
                _byte = _bao.ToArray();
            }
            return _byte;
        }

        override
        public String getType()
        {
            return "[S] S_DropItem";
        }
    }
}